﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Rison.BusinessFacade.Entity.Biz;
using Rison.BusinessFacade.Facade.Biz;
using System.Collections.Generic;
using Rison.BusinessFacade.Common;
using System.Web.SessionState;
using System.Web.UI;
using Rison.DataAccess.Data.Common;
using System.Text;

namespace Rison.Web.wscp.ashx
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class evaluation : IHttpHandler, IRequiresSessionState
    {

        public string Password
        {
            get
            {
                if (HttpContext.Current.Session["pwd"] != null)
                    return HttpContext.Current.Session["pwd"].ToString();

                return "";
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string method = context.Request.Params["method"];
            if (String.IsNullOrEmpty(Password))
            {
                return;
            }
            if ("getDepartments".Equals(method))
            {
                CPResultDetailFacade facade = new CPResultDetailFacade();
                DataSet ds = facade.GetDepartments();
                context.Response.Write(String.Format("var _departments = {0};", JsonCommon.ds2json(ds)));

                DataSet ds1 = loadData(Password);
                context.Response.Write(String.Format("var _data = {0};", JsonCommon.ds2json(ds1)));


            }
            else if ("updateData".Equals(method))
            {

                //判断是否已提交
                string sql_user = String.Format("select if_submit from EvaluationUser where Password={0}", DbAccess.parse(Password));
                int if_submit = (int)(DbAccess.executeQuery(sql_user, "WSCPConnectionString").Tables[0].Rows[0][0]);
                if (if_submit > 0)
                {
                    return;
                }

                WSCPResult result = null;

                List<WSCPResult> results = new List<WSCPResult>();

                string IPAddress = context.Request.UserHostAddress.ToString();


                CPResultDetailFacade facade = new CPResultDetailFacade();
                DataSet ds = facade.GetDepartments();

                string bmid = "";
                string bmmc = "";
                StringBuilder sql = new StringBuilder();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    bmid = ds.Tables[0].Rows[i][0].ToString();
                    bmmc = ds.Tables[0].Rows[i][1].ToString();


                    string yfxz = context.Request.Params[bmid + "_0"];
                    string xzxn = context.Request.Params[bmid + "_1"];
                    string zwgk = context.Request.Params[bmid + "_2"];
                    string fwtd = context.Request.Params[bmid + "_3"];
                    string qzlj = context.Request.Params[bmid + "_4"];
                    string gzcx = context.Request.Params[bmid + "_5"];
                    string suggestion = context.Request.Params["suggestion"];


                    DataRow dr = facade.GetWSCPbydepart(Password, bmid);

                    if (dr == null)
                    {
                        result = new WSCPResult();

                        result.BMGuid = bmid;
                        result.BMMC = bmmc;
                        result.YFXZ = yfxz;
                        result.XZXN = xzxn;
                        result.ZWGK = zwgk;
                        result.FWTD = fwtd;
                        result.QZLJ = qzlj;
                        result.GZCX = gzcx;
                        result.Suggestion = suggestion;
                        result.FromType = cpType.密码登陆.ToString();
                        result.EvaluationUserPassword = Password;
                        result.EvaluationUserIP = IPAddress;
                        results.Add(result);
                    }
                    else
                    {
                        string request_id = context.Request.Params[bmid + "_requestId"];

                        sql.Append(String.Format("update EvaluationOnline  set yfxz='{0}', xzxn='{1}', zwgk='{2}', fwtd='{3}', qzlj='{4}', gzcx='{5}', suggestion={6} where requestid='{7}' ", yfxz, xzxn, zwgk, fwtd, qzlj, gzcx, DbAccess.parse(suggestion), request_id));
                        
                    }
                }
                if (sql.Length > 0)
                {

                    upadteEvaluation(sql.ToString());
                    //如果是提交按钮，则修改提交标志位
                    string is_submit = context.Request.Params["is_submit"];
                    if (!string.IsNullOrEmpty(is_submit))
                    {
                        string up_sql = "update EvaluationUser set if_submit = 1 where Password=" + DbAccess.parse(Password);
                        DbAccess.executeUpdate(up_sql, "WSCPConnectionString");
                    }
                }
                else
                {
                    bool success = facade.Save(results, IPAddress, true);
                }
                context.Response.Write("{success:true,msg:'保存成功！'}");
            }
            else
            {
                context.Response.Write("Hello World");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public DataSet loadData(string password)
        {
            string sql = String.Format("select t2.requestid as request_id, t2.yfxz, t2.xzxn, t2.zwgk, t2.fwtd, t2.qzlj, t2.gzcx, t2.suggestion, t3.id as bmid from request t1, evaluationonline t2, mailbox t3 where t1.id=t2.requestid and t1.mailboxid = t3.id and t1.evaluationUserPassword={0}", DbAccess.parse(password));
            return DbAccess.executeQuery(sql, "WSCPConnectionString");
        }
        public int upadteEvaluation(string sql)
        {
            return DbAccess.executeUpdate(sql, "WSCPConnectionString");
        }
    }
}
